/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 *
 */
import * as path from 'path';

import { ProjectBuildProfile } from '../../../../src/options/build/project-build-profile';
import { updateElementInArrayByUniqueKey } from '../common';

import { BaseModifier } from './base-modifier';
import AppBuildOpt = ProjectBuildProfile.AppBuildOpt;
import BuildMode = ProjectBuildProfile.BuildMode;
import ModuleBuildOpt = ProjectBuildProfile.ModuleBuildOpt;
import ProductBuildOpt = ProjectBuildProfile.ProductBuildOpt;
import ProjectProfileOpt = ProjectBuildProfile.ProjectProfileOpt;
import ProjectTargetBuildOpt = ProjectBuildProfile.ProjectTargetBuildOpt;
import SigningConfigBuildOpt = ProjectBuildProfile.SigningConfigBuildOpt;

/**
 * 修改工程级别的build-profile.json5
 */
export class ProjectBuildProfileModifier extends BaseModifier {
  protected jsonObject!: ProjectProfileOpt;

  constructor(cwd: string) {
    super(path.resolve(cwd, 'build-profile.json5'));
  }

  public setApp(app: AppBuildOpt) {
    this.jsonObject.app = app;
    return this;
  }

  public updateSignConfig(signConfig: SigningConfigBuildOpt) {
    this.jsonObject.app.signingConfigs = updateElementInArrayByUniqueKey(this.jsonObject.app.signingConfigs, signConfig);
    return this;
  }

  public setSdkVersion(version: number) {
    this.jsonObject.app.compileSdkVersion = version;
    return this;
  }

  public setRuntimeInfo(runtimeOS: string, compileVersion: number | string, compatibleVersion: number | string) {
    delete this.jsonObject.app.compileSdkVersion;
    delete this.jsonObject.app.compatibleSdkVersion;
    this.jsonObject.app.products?.forEach(opt => {
      opt.runtimeOS = runtimeOS;
      opt.compileSdkVersion = compileVersion;
      opt.compatibleSdkVersion = compatibleVersion;
    });
    return this;
  }

  public setCompatibleSdkVersion(version: number) {
    this.jsonObject.app.compatibleSdkVersion = version;
    return this;
  }

  public getProducts(): ProductBuildOpt[] | undefined {
    return this.jsonObject.app.products;
  }

  public updateProduct(product: any) {
    this.jsonObject.app.products = updateElementInArrayByUniqueKey(this.jsonObject.app.products, product);
    return this;
  }

  public addProduct(product: any) {
    this.jsonObject.app.products?.push(product);
    return this;
  }

  public updateBuildMode(buildMode: BuildMode) {
    this.jsonObject.app.buildModeSet = updateElementInArrayByUniqueKey(this.jsonObject.app.buildModeSet, buildMode);
    return this;
  }

  public addBuildMode(buildMode: BuildMode) {
    if (this.jsonObject.app.buildModeSet) {
      this.jsonObject.app.buildModeSet.push(buildMode);
    } else {
      this.jsonObject.app.buildModeSet = [buildMode];
    }
    return this;
  }

  public setModules(modules: ModuleBuildOpt[]) {
    this.jsonObject.modules = modules;
    return this;
  }

  public updateModule(module: ModuleBuildOpt) {
    this.jsonObject.modules = updateElementInArrayByUniqueKey(this.jsonObject.modules, module);
    return this;
  }

  public updateModuleTargetByName(target: ProjectTargetBuildOpt, name: string) {
    this.jsonObject.modules.filter(module => {
      return module.name === name;
    }).forEach(module => {
      module.targets = updateElementInArrayByUniqueKey(module.targets, target);
    });
    return this;
  }
}
